<!-- 搜索表单 -->
<template>
  <a-card :bordered="false" :body-style="{ padding: 0 }">
    <a-form
      ref="test"
      :label-col="{
        xl: { span: 10 },
        lg: { span: 10 },
        md: { span: 10 },
        sm: { span: 10 }
      }"
      :wrapper-col="{
        xl: { span: 17 },
        lg: { span: 19 },
        md: { span: 17 },
        sm: { span: 20 }
      }"
    >
      <a-row>
        <a-col
          v-for="(item, index) in firstArr"
          :key="index"
          :xl="6"
          :lg="12"
          :md="12"
          :sm="24"
          :xs="24"
        >
          <zbFormItem
            v-model:value="where[item.name]"
            :dataValue="where[item.name]"
            :itemData="item"
          />
        </a-col>
        <a-col
          v-show="searchExpand"
          :xl="6"
          :lg="12"
          :md="12"
          :sm="24"
          :xs="24"
          v-for="(item, index) in lastArr"
          :key="index"
        >
          <zbFormItem
            v-model:value="where[item.name]"
            :dataValue="where[item.name]"
            :itemData="item"
          />
        </a-col>
        <a-col :xl="6" :lg="12" :md="12" :sm="24" :xs="24">
          <a-form-item class="ele-text-right" :wrapper-col="{ span: 24 }">
            <a-space>
              <a-button type="primary" @click="search">查询</a-button>
              <a-button @click="reset">重置</a-button>
              <a @click="toggleExpand">
                <span v-if="searchExpand">
                  收起 <up-outlined class="ele-text-small" />
                </span>
                <span v-else>
                  展开 <down-outlined class="ele-text-small" />
                </span>
              </a>
            </a-space>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-card>
</template>

<script setup>
  import { reactive, ref } from 'vue';
  import { DownOutlined, UpOutlined } from '@ant-design/icons-vue';
  import useSearch from '@/utils/use-search';
  import zbFormItem from './zb-fotm-item.vue';

  const emit = defineEmits(['search', 'expand-change']);
  const props = defineProps({
    searchData: Array
  });
  let formData = reactive({});
  props.searchData.map((item) => {
    item.value = '';
    formData[item.name] = '';
  });
  // 默认显示的三个筛选条件
  const firstArr = props.searchData.slice(0, 3);
  // // 默认隐藏的筛选条件
  const lastArr = props.searchData.slice(3, props.searchData.length);

  // 表单数据
  const { where, resetFields } = useSearch({
    ...formData
  });

  // 搜索表单是否展开
  const searchExpand = ref(false);

  /* 搜索 */
  const search = () => {
    console.log(where);
    emit('search', where);
  };

  /*  重置 */
  const reset = () => {
    resetFields();
    console.log(where);
    search();
  };

  /* 搜索展开/收起 */
  const toggleExpand = () => {
    searchExpand.value = !searchExpand.value;
    emit('expand-change', searchExpand.value);
  };
</script>
